using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public static PlayerController instance;
    
    [SerializeField] private Rigidbody2D theRB;
    [SerializeField] private float moveSpeed;
    [SerializeField] private InputActionReference moveInput, actionInput;
    [SerializeField] private Animator anim;
    [SerializeField] private float toolWaitTime = .5f;
    [SerializeField] private Transform toolIndicator;
    [SerializeField] private float toolRange = 3f;
    
    private float toolWaitCounter;
    
    public enum ToolType
    {
        // 犁，用于耕地
        plough,
        // 水壶，用于浇水
        wateringCan,
        // 种子，用于播种
        seeds,
        // 篮子，用于收获
        basket

    }
    public ToolType currentTool;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        UIController.instance.SwitchTool((int)currentTool);
    }

    private void Update()
    {
        if (toolWaitCounter > 0f)
        {
            toolWaitCounter -= Time.deltaTime;
            theRB.linearVelocity = Vector2.zero;
        }
        else
        {        
            theRB.linearVelocity = moveInput.action.ReadValue<Vector2>().normalized * moveSpeed;

            if (theRB.linearVelocity.x < 0f)    //对角色进行翻转控制
            {
                transform.localScale = new Vector3(-1f, 1f, 1f);
            }
            else if (theRB.linearVelocity.x > 0f)
            {
                transform.localScale = Vector3.one;
            }
        }

        bool hasSwitchedTool = false;

        if (Keyboard.current.tabKey.wasPressedThisFrame)
        {
            currentTool++;

            if ((int)currentTool >= 4)
            {
                currentTool = ToolType.plough;
            }

            hasSwitchedTool = true;
        }

        if (Keyboard.current.digit1Key.wasPressedThisFrame)
        {
            currentTool = ToolType.plough;

            hasSwitchedTool = true;
        }
        if (Keyboard.current.digit2Key.wasPressedThisFrame)
        {
            currentTool = ToolType.wateringCan;

            hasSwitchedTool = true;
        }
        if (Keyboard.current.digit3Key.wasPressedThisFrame)
        {
            currentTool = ToolType.seeds;

            hasSwitchedTool = true;
        }
        if (Keyboard.current.digit4Key.wasPressedThisFrame)
        {
            currentTool = ToolType.basket;

            hasSwitchedTool = true;
        }

        if (hasSwitchedTool)
        {
            //FindFirstObjectByType<UIController>().SwitchTool((int)currentTool);
            UIController.instance.SwitchTool((int)currentTool);
        }

        if (actionInput.action.WasPressedThisFrame())
        {
            UseTool();
        }
        
        anim.SetFloat("speed", theRB.linearVelocity.magnitude);
        
        toolIndicator.position = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());
        toolIndicator.position = new Vector3(toolIndicator.position.x, toolIndicator.position.y, 0f);

        if (Vector3.Distance(toolIndicator.position, transform.position) > toolRange)
        {
            Vector2 distance = toolIndicator.position - transform.position;
            distance = distance.normalized * toolRange;
            toolIndicator.position = transform.position + new Vector3(distance.x, distance.y, 0f);
        }
        
        toolIndicator.position = new Vector3(Mathf.FloorToInt(toolIndicator.position.x) + 0.5f,
            Mathf.FloorToInt(toolIndicator.position.y) + 0.5f,
            0f);
    }

    void UseTool()
    {
        GrowBlock block = null;
        //block = FindFirstObjectByType<GrowBlock>();
        //block.PloughSoil();
        
        block = GridController.instance.GetBlock(toolIndicator.position.x - 0.5f, toolIndicator.position.y - 0.5f);
        toolWaitCounter = toolWaitTime;

        if (block != null)
        {
            switch (currentTool)
            {
                case ToolType.plough:
                    block.PloughSoil();
                    anim.SetTrigger("usePlough");
                    break;
                case ToolType.wateringCan:
                    block.WaterSoil();
                    anim.SetTrigger("useWateredCan");
                    break;
                case ToolType.seeds:
                    block.PlantCrop();
                    break;
                case ToolType.basket:
                    block.HarvestCrop();
                    break;
            }
        }
    }
}